/*
 *  JDiv - API for Multimedia Application
 *  http://jdiv.sourceforge.net 
 *  ------------------------------------
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software 
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 */

package com.jdiv.functions;

import java.util.List;

import javax.swing.ImageIcon;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.jdiv.FPGManager;
import com.jdiv.JDiv;
import com.jdiv.internalClasses.JDivImage;
import com.jdiv.internalClasses.JDivTexture;
import com.jdiv.kernel.JDivResources;

/** Class for loading and handling Graphics */
public class FunctionsGraphic {
	/**
	  * Load a Texture for the system JDiv.
	  * @param strFile File source of texture. The texture is to be power of 2, in the width and height: 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, ...
	  * @return Number identifier of the Texture or 0 if not could load.
	  */
	
	public static enum MapInfoFlags {G_WIDTH, G_HEIGHT, G_CENTER_X, G_CENTER_Y, G_DEPTH, G_FRAMES, G_ANIMATION_STEP, G_ANIMATION_STEPS, G_ANIMATION_SPEED, G_PITCH}
	
	FPGManager manager;

	 public int loadTexture( int fileId, String strFile )
	 {
		FileHandle file = Gdx.files.internal(strFile);
		
		if( !file.exists() ){
			JDiv.system.log("ERROR, loadTexture() - File not found: '" + strFile + "'" );
			return 0;
		}
		
		return JDivResources.listTextures.textureAdd(fileId, file, false);	
	 }
	 
	 /**
	   * Unload Image stored in JDiv. Be careful when using this function, if you unload the texture can no longer be reused.
	   * @param idImage Number identifier of image.
	   */
	 
	 public void unloadImage( int fileId, int idImage ){
		 JDivResources.listTextures.imageDispose(fileId, idImage);
	 }
	 
	 /**
	   * Unload Texture stored in JDiv. Be careful when using this function, if you unload the texture can no longer be reused.
	   * @param idTexture Number identifier of Texture.
	   */
	 
	 public void unloadTexture(int fileId,  int idTexture ){
		 JDivResources.listTextures.textureDispose( fileId, idTexture );
	 }
	 
	 /**
	  * Load a TextureAtlas for the system JDiv.
	  * @param strFile File source of texture.
	  * @return Number identifier of the Texture or 0 if not could load.
	  */
	 
	 public int loadAtlas( int fileId, String strFile )
	 {
		FileHandle file = Gdx.files.internal(strFile);
			
		if( !file.exists() ){
			JDiv.system.log("ERROR, loadTextureAtlas() - File not found: '" + strFile + "'" );
			return 0;
		}
			
		return JDivResources.listTextures.textureAdd(fileId, file, true);	
	 }
	 
	 /**
	  * Load an Image from Texture to JDiv system. 
	  * @param idTexture Number identifier of Texture. 
	  * @param x X Coordinate in Texture.
	  * @param y Y Coordinate in Texture.
	  * @param width Width of image.
	  * @param height Height of image.
	  * @return Number identifier of image loaded, or 0 if not loaded.
	  */
	 
	 public int loadTextureImage(int fileId,  int idTexture, int x, int y, int width, int height )
	 {
		return JDivResources.listTextures.imageAdd(fileId, idTexture, x, y, width, height);	
	 }
	 
	 /**
	  * Load an Image from Texture to JDiv system. 
	  * @param strFile File source of texture. The texture is to be power of 2, in the width and height: 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, ...
	  * @param x X Coordinate in Texture.
	  * @param y Y Coordinate in Texture.
	  * @param width Width of image.
	  * @param height Height of image.
	  * @return Number identifier of image loaded, or 0 if not loaded.
	  */

	 public int loadFpg(String strFile) {

		 if (manager == null)
			 manager = new FPGManager();
		 
		 return manager.loadFpgJDiv(strFile);
		 
	 }
	 
	 public int loadPng(String strFile) {
		 
			ImageIcon i = new ImageIcon(strFile);
			int h = i.getIconHeight();
			int w = i.getIconWidth();
			int idTexture = loadTexture(0, strFile );
			return JDivResources.listTextures.imageAdd(0, idTexture, 0, 0, w, h);

	 }
	 
	 public int loadTextureImage( String strFile, int x, int y, int width, int height )
	 {
		int idTexture = loadTexture( 0, strFile );
		return JDivResources.listTextures.imageAdd(0, idTexture, x, y, width, height);	
	 }
	 
	 /**
	  * Load an Image from Texture in Atlas to JDiv system.
	  * @param idAtlas Number identifier of Texture.
	  * @param strAtlas String identifier of Image in Atlas.
	  * @return Number identifier of image loaded, or 0 if not loaded.
	  */
	 
	 public int loadAtlasImage( int fileId, int idAtlas, String strAtlas )
	 {
		return JDivResources.listTextures.imageAdd(fileId, idAtlas, strAtlas); 
	 }
	 
	 /**
	  * Load Images from Texture in Atlas to JDiv system.
	  * @param idAtlas Number identifier of Texture.
	  * @param strId Text identifier of images, for example in Atlas: "test001" Text identifier is "test".
	  * @param idFirstImage Number identifier of first image, for example first image is 44, if your text identifier is "test" load from Atlas "test044".
	  * @param length Number of images to load, (idFirstImage + Length - 1) maximum it's 999 images.
	  * @return Number identifier of first image loaded, or 0 if not load.
	  */
	 
	 public int loadAtlasImages( int fileId, int idAtlas, String strId, int idFirstImage, int length )
	 {
		int firstImage = 0;
		int image = 0;
		
		if( (idFirstImage + length - 1) > 999 ) return 0;
		
		String strAtlas;
		
		for( int i = 0; i < length; i++ ){
			strAtlas = strId;
			
			image = idFirstImage + i;
			
			if( image < 10 ) strAtlas = strAtlas + "00" + image;
			else if( image < 100 ) strAtlas = strAtlas + "0" + image;
			else strAtlas = strAtlas + image;
			
			image = JDivResources.listTextures.imageAdd(fileId, idAtlas, strAtlas);
			
			if( firstImage == 0 ) firstImage = image;
		}
		
		return firstImage; 
	 }
	 
	 /**
	  * Gets the color at a point "x, y" of a texture.
	  * @param idTexture Number identifier of Texture. 
	  * @param x X Coordinate in Texture.
	  * @param y Y Coordinate in Texture.
	  * @return RGBA Color.
	  */
	 
	 public int getPixel( int fileId, int idTexture, float x, float y ){
		 JDivTexture jdivTexture = JDivResources.listTextures.getJDivTexture(fileId, idTexture);
		 
		 if( jdivTexture == null ) return -1;
		 
		 return jdivTexture.getPixel((int)x, (int)y);
	 }
	 
	 /**
	  * Gets the number specified by the color (r, g, b, a).
	  * @param r Red component, 0 - 255. 
	  * @param g Green component, 0 - 255.
	  * @param b Blue component, 0 - 255.
	  * @param a Alpha component, 0 - 255.
	  * @return The color in rgba8888 format.
	  */
	 
	 public int getColorInt( int r, int g, int b, int a){
		 return Color.rgba8888( ((float)r)/255, ((float)g)/255, ((float)b)/255, ((float)a)/255);
	 }
	 
	 /**
	  * Gets the number specified by the color (r, g, b, a).
	  * @param r Red component, 0 - 1. 
	  * @param g Green component, 0 - 1.
	  * @param b Blue component, 0 - 1.
	  * @param a Alpha component, 0 - 1.
	  * @return The color in rgba8888 format.
	  */
	 
	 public int getColorFloat( float r, float g, float b, float a){
		 return Color.rgba8888( r, g, b, a);
	 }
	 
	 /**
	  * @param idImage Number identifier of image.
	  * @return Gets the width of the image.
	  */
	 
	 public int getImageWidth(int fileId,  int idImage ){
		 JDivImage jdivImage = JDivResources.listTextures.getJDivImage(fileId, idImage);
		 
		 if( jdivImage != null ){
			 
			 // Si no se cargo la imagen la cargamos
			 if( jdivImage.width == 0 )
				 jdivImage.getData();
			 
			 return jdivImage.width;
		 }else
			 return 0;
	 }
	 
	 /**
	  * @param idImage Number identifier of image.
	  * @return Gets the height of the image.
	  */
	 
	 public int getImageHeight(int fileId, int idImage ){
         JDivImage jdivImage = JDivResources.listTextures.getJDivImage(fileId, idImage);
		 
		 if( jdivImage != null ){
			 // Si no se cargo la imagen la cargamos
			 if( jdivImage.width == 0 )
				 jdivImage.getData();
			 
			 return jdivImage.height;
		 }else
			 return 0;
	 }
	 
	 /**
	  * Gets an Image LibGdx.
	  * @param idImage Number identifier of image.
	  * @param mirror Mirroring the graph to show, initial its "Const.MIRROR_NONE." 
	   *  <p>This variable describes the form of the graph:</p>
	   *  <p>{@link com.jdiv.Const#MIRROR_NONE}: Not mirror.
	   *  <p>{@link com.jdiv.Const#MIRROR_HORIZONTAL}: Horizontal mirror.
	   *  <p>{@link com.jdiv.Const#MIRROR_VERTICAL}: Vertical mirror.
	   *  <p></p>
	   *  <p>Example of horizontal and vertical mirror: mirror = Const.MIRROR_HORIZONTAL | Const.MIRROR_VERTICAL;
	   
	  * @return LibGdx image.
	  */
	 
	 public Image getImage(int fileId,  int idImage, int mirror ){
         JDivImage jdivImage = JDivResources.listTextures.getJDivImage(fileId, idImage);
         
         Image image = null;
		 
		 if( jdivImage != null ){
			 // Si no se cargo la imagen la cargamos
			 if( jdivImage.width == 0 )
				 jdivImage.getData();
			 
			 image = jdivImage.getImage(mirror);
		 }
		 
		 if( image == null )
			 JDiv.system.log("getImage() - Before using this function must call to 'frame()' so that the load is made.");

	 	 return image; 
	 }
	 
	 /**
	  * Gets a Color in LibGdx.
	  * @param rgba The color in rgba8888 format.
	  * @return LibGdx Color.
	  */
	 
	 public Color rgba8888ToColor( int rgba ){
		  Color colorSet = new Color();
		  Color.rgba8888ToColor(colorSet, rgba);
		  return colorSet;
	 }
	 
	 public int mapInfo(int fileId, int graphId, MapInfoFlags flags) {
		 
		 int ret = 0;
		 
		 JDivImage image = JDivResources.listTextures.getJDivImage(fileId, graphId);
		 
		 if (image != null) {
			 
			 if (image.getTextureRegion() != null) {
				 if (flags == MapInfoFlags.G_WIDTH)
					 
					 ret = image.getTextureRegion().getRegionWidth();
				 
				 else if (flags == MapInfoFlags.G_HEIGHT)
					 
					 ret = image.getTextureRegion().getRegionHeight();
				 
				 else if (flags == MapInfoFlags.G_DEPTH)
					 
					 ret = 32;
			 }
			 
		 }
		 
		 return ret;
		 
	 }
	 
}
